import { defineStore } from 'pinia'
import categoryApi, { type CategoryModel } from '@/api/category'
import { ElMessage } from 'element-plus'


interface StateModel {
    category1Id: number | undefined,
    category2Id: number | undefined,
    category3Id: number | undefined,
    category1List: CategoryModel[],
    category2List: CategoryModel[],
    category3List: CategoryModel[],
}
const initState = (): StateModel => ({
    category1Id: undefined,
    category2Id: undefined,
    category3Id: undefined,
    category1List: [],
    category2List: [],
    category3List: [],
})
const useCategoryStore = defineStore('category', {
    state() {
        return initState()
    },
    actions: {
        async getCategory1List() {
            try {
                let result = await categoryApi.getCategory1List()
                this.category1List = result
            } catch (error) {
                ElMessage.error('获取一级分类数据失败')
            }
        },
        async getCategory2List() {
            try {
                this.category2Id = undefined;
                this.category3Id = undefined;
                this.category2List = [];
                this.category3List = [];
                let result = await categoryApi.getCategory2List(this.category1Id as number)
                this.category2List = result
            } catch (error) {
                ElMessage.error('获取一级分类数据失败')
            }
        },
        async getCategory3List() {
            try {
                this.category3Id = undefined;
                this.category3List = [];
                let result = await categoryApi.getCategory3List(this.category2Id as number)
                this.category3List = result
            } catch (error) {
                ElMessage.error('获取一级分类数据失败')
            }
        }
    },
    getters: {

    }
})

export default useCategoryStore